<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>五子棋在线对战 - 注册</title>
    <link rel="stylesheet" href="common.css">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .register-container {
            width: 100%;
            max-width: 450px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .register-header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .register-header h1 {
            color: white;
            font-size: 28px;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }
        
        .register-header p {
            color: rgba(255,255,255,0.8);
            font-size: 14px;
        }
        
        .register-form {
            background: white;
            border-radius: 12px;
            padding: 40px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }
        
        .form-group {
            margin-bottom: 25px;
        }
        
        .form-label {
            color: #2c3e50;
            font-weight: 600;
            margin-bottom: 8px;
        }
        
        .form-input {
            width: 100%;
            padding: 15px;
            border: 2px solid #e1e8ed;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
        }
        
        .form-input:focus {
            border-color: #27ae60;
            box-shadow: 0 0 0 3px rgba(39, 174, 96, 0.1);
        }
        
        .password-strength {
            margin-top: 8px;
            font-size: 12px;
        }
        
        .strength-weak { color: #e74c3c; }
        .strength-medium { color: #f39c12; }
        .strength-strong { color: #27ae60; }
        
        .register-btn {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #27ae60, #2ecc71);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 20px;
        }
        
        .register-btn:hover {
            background: linear-gradient(135deg, #229954, #27ae60);
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }
        
        .register-btn:disabled {
            background: #bdc3c7;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }
        
        .login-link {
            text-align: center;
            margin-top: 20px;
        }
        
        .login-link a {
            color: #3498db;
            text-decoration: none;
            font-weight: 500;
        }
        
        .login-link a:hover {
            text-decoration: underline;
        }
        
        .footer {
            text-align: center;
            margin-top: 30px;
            color: rgba(255,255,255,0.7);
            font-size: 12px;
        }
        
        .error-message {
            background: #fadbd8;
            color: #e74c3c;
            padding: 12px;
            border-radius: 6px;
            margin-bottom: 20px;
            border-left: 4px solid #e74c3c;
        }
        
        .success-message {
            background: #d5f4e6;
            color: #27ae60;
            padding: 12px;
            border-radius: 6px;
            margin-bottom: 20px;
            border-left: 4px solid #27ae60;
        }
        
        .validation-message {
            font-size: 12px;
            margin-top: 5px;
        }
        
        .validation-success {
            color: #27ae60;
        }
        
        .validation-error {
            color: #e74c3c;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <div class="register-header">
            <h1>五子棋在线对战</h1>
            <p>创建账号，开始你的对弈之旅</p>
        </div>
        
        <div class="register-form fade-in">
            <form id="registerForm">
                <div class="form-group">
                    <label class="form-label" for="nickname">游戏昵称</label>
                    <input type="text" id="nickname" class="form-input" placeholder="请输入游戏昵称（2-20个字符）" required>
                    <div id="nicknameValidation" class="validation-message"></div>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="username">账号</label>
                    <input type="text" id="username" class="form-input" placeholder="请输入账号（6-20位字符）" required>
                    <div id="usernameValidation" class="validation-message"></div>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="password">密码</label>
                    <input type="password" id="password" class="form-input" placeholder="请输入密码（至少8位，包含字母和数字）" required>
                    <div id="passwordStrength" class="password-strength"></div>
                    <div id="passwordValidation" class="validation-message"></div>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="confirmPassword">确认密码</label>
                    <input type="password" id="confirmPassword" class="form-input" placeholder="请再次输入密码" required>
                    <div id="confirmPasswordValidation" class="validation-message"></div>
                </div>
                
                <div id="errorMessage" class="error-message hidden"></div>
                <div id="successMessage" class="success-message hidden"></div>
                
                <button type="submit" id="registerBtn" class="register-btn" disabled>注册</button>
            </form>
            
            <div class="login-link">
                <p>已有账号？ <a href="login.html">立即登录</a></p>
            </div>
        </div>
        
        <div class="footer">
            <p>© 2024 五子棋在线对战</p>
        </div>
    </div>

    <script>
        const nicknameInput = document.getElementById('nickname');
        const usernameInput = document.getElementById('username');
        const passwordInput = document.getElementById('password');
        const confirmPasswordInput = document.getElementById('confirmPassword');
        const registerBtn = document.getElementById('registerBtn');
        
        // 昵称验证
        nicknameInput.addEventListener('input', function() {
            const nickname = this.value;
            const validation = document.getElementById('nicknameValidation');
            
            if (nickname.length === 0) {
                validation.textContent = '';
                validation.className = 'validation-message';
                updateRegisterButton();
                return;
            }
            
            if (nickname.length < 2) {
                validation.textContent = '昵称至少需要2个字符';
                validation.className = 'validation-message validation-error';
                updateRegisterButton();
                return;
            }
            
            if (nickname.length > 20) {
                validation.textContent = '昵称不能超过20个字符';
                validation.className = 'validation-message validation-error';
                updateRegisterButton();
                return;
            }
            
            // 检查是否包含特殊字符
            if (!/^[\u4e00-\u9fa5a-zA-Z0-9_]+$/.test(nickname)) {
                validation.textContent = '昵称只能包含中文、字母、数字和下划线';
                validation.className = 'validation-message validation-error';
                updateRegisterButton();
                return;
            }
            
            validation.textContent = '✓ 昵称格式正确';
            validation.className = 'validation-message validation-success';
            updateRegisterButton();
        });
        
        // 用户名验证
        usernameInput.addEventListener('input', function() {
            const username = this.value;
            const validation = document.getElementById('usernameValidation');
            
            if (username.length < 6) {
                validation.textContent = '用户名至少需要6个字符';
                validation.className = 'validation-message validation-error';
                updateRegisterButton();
                return;
            }
            
            if (username.length > 20) {
                validation.textContent = '用户名不能超过20个字符';
                validation.className = 'validation-message validation-error';
                updateRegisterButton();
                return;
            }
            
            // 检查是否只包含字母、数字和下划线
            if (!/^[a-zA-Z0-9_]+$/.test(username)) {
                validation.textContent = '用户名只能包含字母、数字和下划线';
                validation.className = 'validation-message validation-error';
                updateRegisterButton();
                return;
            }
            
            // 检查账号唯一性（模拟异步验证）
            validation.textContent = '正在检查账号可用性...';
            validation.className = 'validation-message';
            
            // 模拟服务器验证延迟
            setTimeout(() => {
                checkUsernameAvailability(username);
            }, 500);
        });
        
        // 模拟账号唯一性检查
        function checkUsernameAvailability(username) {
            const validation = document.getElementById('usernameValidation');
            
            // 模拟已存在的账号列表
            const existingUsernames = ['admin', 'test', 'user123', 'player001', 'demo'];
            
            if (existingUsernames.includes(username.toLowerCase())) {
                validation.textContent = '该账号已被使用，请选择其他账号';
                validation.className = 'validation-message validation-error';
            } else {
                validation.textContent = '✓ 账号可用';
                validation.className = 'validation-message validation-success';
            }
            
            updateRegisterButton();
        }
        
        // 密码强度检测
        passwordInput.addEventListener('input', function() {
            const password = this.value;
            const strengthDiv = document.getElementById('passwordStrength');
            const validation = document.getElementById('passwordValidation');
            
            let strength = 0;
            let strengthText = '';
            let strengthClass = '';
            
            if (password.length >= 8) strength++;
            if (/[a-z]/.test(password)) strength++;
            if (/[A-Z]/.test(password)) strength++;
            if (/[0-9]/.test(password)) strength++;
            if (/[^a-zA-Z0-9]/.test(password)) strength++;
            
            if (strength < 3) {
                strengthText = '密码强度：弱';
                strengthClass = 'strength-weak';
            } else if (strength < 4) {
                strengthText = '密码强度：中等';
                strengthClass = 'strength-medium';
            } else {
                strengthText = '密码强度：强';
                strengthClass = 'strength-strong';
            }
            
            strengthDiv.textContent = strengthText;
            strengthDiv.className = `password-strength ${strengthClass}`;
            
            if (password.length < 8) {
                validation.textContent = '密码至少需要8个字符';
                validation.className = 'validation-message validation-error';
            } else if (!/[a-zA-Z]/.test(password) || !/[0-9]/.test(password)) {
                validation.textContent = '密码必须包含字母和数字';
                validation.className = 'validation-message validation-error';
            } else {
                validation.textContent = '✓ 密码格式正确';
                validation.className = 'validation-message validation-success';
            }
            
            // 重新检查确认密码
            if (confirmPasswordInput.value) {
                checkPasswordMatch();
            }
            
            updateRegisterButton();
        });
        
        // 确认密码验证
        confirmPasswordInput.addEventListener('input', checkPasswordMatch);
        
        function checkPasswordMatch() {
            const password = passwordInput.value;
            const confirmPassword = confirmPasswordInput.value;
            const validation = document.getElementById('confirmPasswordValidation');
            
            if (confirmPassword && password !== confirmPassword) {
                validation.textContent = '两次输入的密码不一致';
                validation.className = 'validation-message validation-error';
            } else if (confirmPassword && password === confirmPassword) {
                validation.textContent = '✓ 密码匹配';
                validation.className = 'validation-message validation-success';
            } else {
                validation.textContent = '';
                validation.className = 'validation-message';
            }
            
            updateRegisterButton();
        }
        
        function updateRegisterButton() {
            const nickname = nicknameInput.value;
            const username = usernameInput.value;
            const password = passwordInput.value;
            const confirmPassword = confirmPasswordInput.value;
            
            const nicknameValid = nickname.length >= 2 && nickname.length <= 20 && /^[\u4e00-\u9fa5a-zA-Z0-9_]+$/.test(nickname);
            const usernameValid = username.length >= 6 && username.length <= 20 && /^[a-zA-Z0-9_]+$/.test(username);
            const passwordValid = password.length >= 8 && /[a-zA-Z]/.test(password) && /[0-9]/.test(password);
            const confirmValid = confirmPassword && password === confirmPassword;
            
            // 检查账号是否可用（通过验证消息的类名判断）
            const usernameValidation = document.getElementById('usernameValidation');
            const usernameAvailable = usernameValidation.classList.contains('validation-success');
            
            registerBtn.disabled = !(nicknameValid && usernameValid && usernameAvailable && passwordValid && confirmValid);
        }
        
        document.getElementById('registerForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const nickname = nicknameInput.value;
            const username = usernameInput.value;
            const password = passwordInput.value;
            const errorDiv = document.getElementById('errorMessage');
            const successDiv = document.getElementById('successMessage');
            
            // 隐藏之前的消息
            errorDiv.classList.add('hidden');
            successDiv.classList.add('hidden');
            
            // 最终验证
            if (!nickname || !username || !password) {
                showError('请填写所有必填字段');
                return;
            }
            
            // 模拟注册过程
            showSuccess('注册成功！正在跳转到登录页面...');
            
            // 模拟跳转到登录页面
            setTimeout(() => {
                window.location.href = 'login.html';
            }, 2000);
        });
        
        function showError(message) {
            const errorDiv = document.getElementById('errorMessage');
            errorDiv.textContent = message;
            errorDiv.classList.remove('hidden');
        }
        
        function showSuccess(message) {
            const successDiv = document.getElementById('successMessage');
            successDiv.textContent = message;
            successDiv.classList.remove('hidden');
        }
    </script>
</body>
</html>
